#include "lab4_problem2.h"

#include <iostream>
#include <string>

#include "../p1/PowerMethod.h" 
#include "../../utils/MatrixUtils.h"

namespace Lab4
{
    namespace Problem2
    {
        void problem2()
        {
            std::cout << "Lab 4 - Problem 2:" << std::endl;
        
        	// Get the path of the files to build the matrices.
        	static const std::string filePathA1 = "lab4/p2/datos/matrixA1.txt";
        	static const std::string filePathA2 = "lab4/p2/datos/matrixA2.txt";
        	static const std::string filePathx0 = "lab4/p2/datos/initialX.txt";

        	static const uint32_t maxIterations = 100;
        	static const double errorTolerance = 0.00001;
        	
        	// Use the power method to approximate the eigenvalue and eigenvector
        	// of the first matrix.
        	double eigenValue = 0.0;
        	static const uint32_t matrixDimension = 4;
        	Utils::Matriz eigenVector(matrixDimension, 1);
        	uint32_t lastIteration = 0;
        	NumericalAnalysis::PowerMethod(matrixDimension, filePathA1, filePathx0, maxIterations, 
        	                               errorTolerance, eigenValue, eigenVector, lastIteration);

        	Utils::Matriz A1 = Utils::GetMatrixFromFile(filePathA1, matrixDimension, matrixDimension);

        	// Resultado = 9.0
        	std::cout << "First matrix:" << std::endl;
        	std::cout << "Eigenvalue = " << eigenValue << std::endl << std::endl;
        	
        	// Resultado = Transpuesta( [ 1 -8.6278e-006 1.56566e-011 -3.68948e-018 ] )
        	std::cout << "Eigenvector:" << std::endl;
        	Utils::PrintMatrix(eigenVector);

        	// Resultado = 100
        	std::cout << std::endl;
        	std::cout << "Last Iteration = " << lastIteration << std::endl;
        	
        	std::cout << std::endl << std::endl;

        	// Use the power method to approximate the eigenvalue and eigenvector
        	// of the second matrix.
        	NumericalAnalysis::PowerMethod(matrixDimension, filePathA2, filePathx0, maxIterations,
        	                               errorTolerance, eigenValue, eigenVector, lastIteration);

        	Utils::Matriz A2 = Utils::GetMatrixFromFile(filePathA2, matrixDimension, matrixDimension);

        	// Resultado = 9.0
        	std::cout << "Second matrix:" << std::endl;
        	std::cout << "Eigenvalue = " << eigenValue << std::endl << std::endl;

        	// Resultado = Transpuesta( [ 1 -8.6278e-006 1.56566e-011 -1 ] ) 
        	std::cout << "Eigenvector:" << std::endl;
        	Utils::PrintMatrix(eigenVector);

        	// Resultado = 100
        	std::cout << std::endl;
        	std::cout << "Last Iteration = " << lastIteration << std::endl;

        	std::cout << std::endl << std::endl;
        }
    }
}
